package com.base.library.view.galleryImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.base.library.R;
import com.base.library.application.BaseApplication;
import com.base.library.util.CodeUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * 作者：王东一 on 2015/8/6 13:30
 */
public class UrlTouchImageView extends RelativeLayout {
    protected ImageView mImageView;
    protected Context mContext;
    public PhotoViewAttacher mAttacher;
    public ArrayList<HashMap<String, Object>> pictureList = new ArrayList<>();
    protected DisplayImageOptions optionsRectangular = null;
    public ImageLoader mImageLoader;

    public UrlTouchImageView(Context ctx) {
        super(ctx);
        mContext = ctx;
        Point();
        init();

    }

    public UrlTouchImageView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        mContext = ctx;
        Point();
        init();
    }

    public ImageView getImageView() {
        return mImageView;
    }

    protected void init() {
        mImageView = new ImageView(mContext);
        mImageView.setScaleType(ScaleType.CENTER);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mImageView.setLayoutParams(params);
        mAttacher = new PhotoViewAttacher(mImageView);
        this.addView(mImageView);
        mImageView.setVisibility(VISIBLE);
    }

    private void Point() {
        mImageLoader = ImageLoader.getInstance();
        optionsRectangular = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
    }

    public void setUrl(String imageUrl) {
        mImageLoader.loadImage(imageUrl, optionsRectangular, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                mImageView.setImageResource(R.drawable.no_date);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                mImageView.setImageBitmap(loadedImage);
                mAttacher.update();
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        //  Auto-generated method stub
        super.setOnClickListener(l);
        mImageView.setOnClickListener(l);
    }

    // No caching load
    public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... strings) {
            String url = strings[0];
            Bitmap bm = null;
            try {
                URL aURL = new URL(url);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                int totalLen = conn.getContentLength();
                InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen);
//                bis.setProgressListener(new InputStreamWrapper.InputStreamProgressListener() {
//                    @Override
//                    public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) {
//                        publishProgress((int) (progressValue * 100));
//                    }
//                });
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            HashMap<String, Object> bitmap = new HashMap<>();
            bitmap.put(url, bm);
            bitmap.put("url", url);
            pictureList.add(bitmap);
            return bm;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                mImageView.setScaleType(ScaleType.CENTER);
                bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.touxiang);
                mImageView.setImageBitmap(bitmap);
            } else {
                mImageView.setScaleType(ScaleType.MATRIX);
                mImageView.setImageBitmap(bitmap);
            }
            mImageView.setVisibility(VISIBLE);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
//            mProgressBar.setProgress(values[0]);
        }
    }
}
